package com.aounhui.productor.controller;

import com.aounhui.common.entity.Result;
import com.aounhui.productor.entity.Commodity;
import com.aounhui.productor.entity.CommodityExample;
import com.aounhui.productor.entity.CommodityFactory;
import com.aounhui.productor.mapper.CommodityMapper;
import com.aounhui.productor.service.CommodityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author aounhui
 * @version 1.0
 * @date 2021/6/17 9:31
 */
@RestController
@Slf4j
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    @GetMapping(value = "/productor/commodity/search")
    public Result getCommodity(@RequestParam String commodityName,
                               @RequestParam int number,
                               @RequestParam String orderId) {
        try {
            log.info("查询{} 库存", commodityName);
            List<Commodity> commodities = commodityService.getCommodity(commodityName, number, orderId);
            return Result.ok(commodities);
        } catch (Exception e) {
            log.error("查询{} 库存失败",e);
            return Result.error("查询库存失败");
        }
    }

    @Autowired
    private CommodityMapper commodityMapper;
    @PostMapping(value = "/productor/test")
    public Result test(){
        CommodityFactory commodityFactory = new CommodityFactory();
        commodityFactory.setName("milk");
        commodityFactory.setExpireDate(12);
        commodityService.commodityCreater(commodityFactory);
        System.out.println("完事");
        return Result.ok();
    }
}
